package com.yongjie.springbootlogin.controller;

import com.alibaba.fastjson.JSON;
import com.yongjie.springbootlogin.entity.DoctorInfo;
import com.yongjie.springbootlogin.entity.PesonInfo;
import com.yongjie.springbootlogin.entity.Registration;


import com.yongjie.springbootlogin.service.Interface.RegistrationService;
import com.yongjie.springbootlogin.service.PesonInfoService;
import com.yongjie.springbootlogin.service.RegistrationServiceImpl;
import com.yongjie.springbootlogin.utils.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**挂号
 * @author 13468
 */
@Controller
public class registrationController {
    @Autowired
    private RegistrationServiceImpl Rservice;
    @Autowired
    PesonInfoService pesonInfoService;

    @RequestMapping("/Registration")
    public String Registration(Map<String,Object>map){
        //医生信息
        List<DoctorInfo> doctorInfos = Rservice.selcDoctor();
        map.put("doctorInfos",doctorInfos);
        return "Registration/AddReistration";
    }

    /**
     * 添加数据
     */
    @PostMapping(value = "/AddRegistration")
    @ResponseBody
    public ResultMessage addRegistration(
                                  @RequestParam("paiName") String paiName,
                                  @RequestParam("paiPhone") String paiPhone,
                                  @RequestParam("paiMeal") String paiMeal,
                                  @RequestParam("docId") String docId,
                                  @RequestParam("time")String time
    ) {
        ResultMessage resultMessage=null;
        int count = Rservice.inseraddData(paiName, paiPhone, paiMeal, docId, time);
        if(count>0){
            resultMessage=new ResultMessage(200,"添加成功");
        }else{
            resultMessage=new ResultMessage(500,"添加失败");
        }
//        List<DoctorInfo> doctors = Rservice.selcDoctor();
//        String json = JSON.toJSONString(doctors);
//        response.getWriter().println(json);

        return resultMessage;

    }

    /**
     *挂号记录列表（医生）
     */
   /* @RequestMapping(value = "/ListRegistration")
    public String ListRegistration(Map<String ,Object>map,Registration registration){
        //获取挂号记录
        List<Registration> rGlist=  Rservice.ListRegistration();
        System.out.println(rGlist);
        map.put("rts",rGlist);
        return "Registration/RegistrationList";
    }*/
    /**
     * 挂号记录分页
     * @return
     */
    @RequestMapping(value = "/ListRegistration")
    public String allList(Map<String,Object>map,
                          HttpServletRequest request
    ){
        //获取数据总条数
        Integer count = Rservice.RegistrationCount();
        map.put("count",count);
        String cupage=request.getParameter("currentPage");
        int currentPage;//默认当前页码为1
        int pageSize=5;//每页4条数据
        //计算总页数;
        int pages=(count+pageSize-1)/pageSize;
        //获取当前页码
        currentPage=(cupage==null)?1:Integer.parseInt(cupage);
        //如果当前页码小于1，则赋值为1，表示为首页
        if(currentPage<1) {
            currentPage=1;
        }
        //如果当前页数大于总页数，则赋值为总页数值，表示为尾页
        if(currentPage>pages) {
            currentPage=pages;
        }
        //将当前的页码保存在map中
        map.put("currentPage", currentPage);
        map.put("pages", pages);
        currentPage= (currentPage-1)*pageSize;
        if(currentPage<=0){
            currentPage=0;
        }
        //准备分页的数据
        List<Registration> rGlist = Rservice.getRegistrationPages(currentPage, pageSize);
        map.put("rts",rGlist);
        return "Registration/RegistrationList";
    }
    /**
     * 根据查询姓名或电话号码
     */
    @RequestMapping("/selList")
    public String selList(  @RequestParam(value = "regInof",required = false) String pai_name,
                            @RequestParam(value = "regInof",required = false)String pai_phone,
                            Map<String,Object>map ){
        if(pai_name.equals("")){
            pai_name=null;
        }
        if(pai_phone.equals("")){
            pai_phone=null;
        }
        //查询挂号记录
        List<Registration> rGlist=  Rservice.selRegistration("%"+pai_name+"%","%"+pai_phone+"%");
        map.put("rts",rGlist);
        return "Registration/PatientSelectLict";
    }
    /**
     * 获取挂号记录（客户）
     */
    @RequestMapping("/clientAllList")
    public  String clientAllList(Map<String,Object>map,
                                 HttpServletRequest request){
        //获取数据总条数
        Integer count = Rservice.RegistrationCount();
        map.put("count",count);
        String cupage=request.getParameter("currentPage");
        int currentPage;//默认当前页码为1
        int pageSize=5;//每页4条数据
        //计算总页数;
        int pages=(count+pageSize-1)/pageSize;
        //获取当前页码
        currentPage=(cupage==null)?1:Integer.parseInt(cupage);
        //如果当前页码小于1，则赋值为1，表示为首页
        if(currentPage<1) {
            currentPage=1;
        }
        //如果当前页数大于总页数，则赋值为总页数值，表示为尾页
        if(currentPage>pages) {
            currentPage=pages;
        }
        //将当前的页码保存在map中
        map.put("currentPage", currentPage);
        map.put("pages", pages);
        currentPage= (currentPage-1)*pageSize;
        if(currentPage<=0){
            currentPage=0;
        }
        //准备分页的数据
        List<Registration> rGlist = Rservice.getRegistrationPages(currentPage, pageSize);
        map.put("rts",rGlist);
        return "Registration/Registrationindex";
    }

    /**
     * 查询医生关联的挂号记录
     */
    @RequestMapping("/dcList")
    public  String dcList(Map<String ,Object>map,HttpServletRequest request,HttpSession session){
        //获取数据总条数
        Integer count = Rservice.RegistrationCount();
        map.put("count",count);
        String cupage=request.getParameter("currentPage");
        int currentPage;//默认当前页码为1
        int pageSize=5;//每页4条数据
        //计算总页数;
        int pages=(count+pageSize-1)/pageSize;
        //获取当前页码
        currentPage=(cupage==null)?1:Integer.parseInt(cupage);
        //如果当前页码小于1，则赋值为1，表示为首页
        if(currentPage<1) {
            currentPage=1;
        }
        //如果当前页数大于总页数，则赋值为总页数值，表示为尾页
        if(currentPage>pages) {
            currentPage=pages;
        }
        //将当前的页码保存在map中
        map.put("currentPage", currentPage);
        map.put("pages", pages);
        currentPage= (currentPage-1)*pageSize;
        if(currentPage<=0){
            currentPage=0;
        }
        //获取session的id
        String userID = (String) session.getAttribute("userID");
        PesonInfo userinfo= pesonInfoService.userinfo(userID);
        String id = userinfo.getId();
        //准备分页的数据
        List<Registration> rGlist = Rservice.getdcList(currentPage, pageSize,id);
        map.put("rts",rGlist);
        return "Registration/DoctorRegistrationList";
    }
    /**
     * 删除挂号数据
     */
    @RequestMapping("/deleteRegistration")
    @ResponseBody
    public ResultMessage deleteRegistration(String id){
        ResultMessage resultMessage=null;
        int count = Rservice.deleteRegistration(id);
        if (count > 0) {
            resultMessage = new ResultMessage(200, "删除成功");
        } else {
            resultMessage = new ResultMessage(500, "删除失败");
        }
        return  resultMessage;
    }



}
